package com.codechuan.demo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.TokenObject;
import com.codechuan.demo.ui.myToken.MyToken;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.MyHttpUtil;

import java.util.List;
/**
 * @Deprecated 令牌适配器
 */
public class TokenBaseAdapter extends BaseAdapter {

    private Context context;

    private List<TokenObject> tokenObjects;

    public TokenBaseAdapter(Context context, List<TokenObject> tokenObjects) {
        this.context = context;
        this.tokenObjects = tokenObjects;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        // 获取总回显数
        return tokenObjects.size();
    }

    @Override
    public Object getItem(int i) {
        return tokenObjects.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            // 获取布局样式内容,根据布局***.xml 生成视图对象
            view = LayoutInflater.from(context).inflate(R.layout.item_token, null);
            viewHolder = new ViewHolder();
            viewHolder.ll_item = view.findViewById(R.id.ll_item);
            viewHolder.name = view.findViewById(R.id.tk_name);
            viewHolder.desc = view.findViewById(R.id.tk_desc);
            viewHolder.status = view.findViewById(R.id.tk_status);
            viewHolder.button = view.findViewById(R.id.click_button);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
    if (tokenObjects.isEmpty()){
        DialogUtil.showDialog(Constant.context,"查询为0网络问题请重新打开此页面,或您暂为添加小号.");
    }
        //  blocksDescendants 阻止下面的控件获得焦点
        viewHolder.ll_item.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        // 设置Token回显格式
        TokenObject tokenObject = tokenObjects.get(i);
        String tokenName = "token" + (i + 1) + ":" + maskString(tokenObject.getToken());
        viewHolder.name.setText(tokenName);

        // 设置TokenBindPhone回显格式
        String tokenDesc = "手机号:" + maskString(tokenObject.getDesc());
        viewHolder.desc.setText(tokenDesc);

        // 设置TokenStatus回显格式
        String tokenStatus = "状态：" + tokenObject.getStatus();
        viewHolder.status.setText(tokenStatus);

        // 删除按钮点击事件
        viewHolder.button.setOnClickListener(view1 -> {
            DialogUtil.showDialog(Constant.context,"您删除了第" + i + "个token");
            // 删除token
            new Thread(() -> {
                boolean b = MyHttpUtil.deleteUserMinToken(tokenObjects.get(i));
                if (b){
                    Constant.handler.post(()->{
                       DialogUtil.showDialog(Constant.context,"删除成功!");
                    });
                }else {
                    Constant.handler.post(()->{
                        DialogUtil.showDialog(Constant.context,"删除失败!");
                    });
                }
                Intent intent = new Intent();
                intent.setClass(Constant.context , MyToken.class);
                context.startActivity(intent);
            }).start();

        });

        return view;
    }
    public static String maskString(String str) {
        if (str == null || str.length() <= 10) {
            return str;
        }
        return str.substring(0, 8) + "******";
    }
    public static final class ViewHolder {
        public LinearLayout ll_item;

        public TextView name;

        public TextView desc;

        public Button button;
        public TextView status;

    }
}
